package com.jokins.admin.vue.user.action;

import com.jokins.admin.vue.user.actioncontext.SysUserRegisterPasswordDoc;
import com.jokins.admin.vue.user.mapper.SysUserMapper;
import com.jokins.admin.vue.user.param.SysUserRegisterPasswordParam;
import com.jokins.admin.vue.user.utils.FixedHashPasswordUtils;
import com.jokins.common.actions.action.ActionContext;
import com.jokins.common.actions.action.BaseAction;
import com.jokins.common.actions.exception.BusinessException;
import com.jokins.common.entry.vue.user.SysUser;
import com.jokins.common.util.SqlTypeConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class SysUserRegisterPasswordDocAction extends BaseAction<SysUserRegisterPasswordDoc> {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    protected void process(SysUserRegisterPasswordDoc actionContext) throws BusinessException {
        SysUserRegisterPasswordParam param = actionContext.getParam();
        SysUser sysUser = new SysUser();
        String sourcePassword = param.getPassword();
        String newPassword = FixedHashPasswordUtils.sha256Hash(param.getPassword());
        sysUser.setPassword(sourcePassword);
        sysUser.setSalt(newPassword);
        sysUser.setUserCode(param.getUserName());
        SqlTypeConverter.copyEntity(param,sysUser);
        sysUserMapper.insert(sysUser);
        Map<String, String> resultMap = new HashMap<>();
        resultMap.put("password",sourcePassword);
        resultMap.put("userName",newPassword);
        actionContext.setResult(ActionContext.RESULT_DOC_KEY,resultMap);
    }
}
